//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    token: "",
    isSuperAdmin: false
  },

  bindLogin() {
    const that = this
    // 如果用户未登录
    if (this.data.token.length === 0) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else {
      //TODO: 已经登录，点击后退出登录，登录状态设置为false
      wx.showModal({
        title: "提示",
        content: "确定退出登录吗？",
        success(res) {
          if (res.confirm) {
            wx.setStorageSync('token', "")
            that.setData({
              token: ""
            })
          }

        }
      })
    }
  },

  bindBookAdmin() {
    const that = this
    // 如果用户未登录
    if (this.data.token.length === 0) {
      wx.showModal({
        title: "提示",
        content: "请先登录！"
      })
    } else {
      wx.navigateTo({
        url: '../bookAdmin/bookAdmin',
      })
    }
  },

  bindUserAdmin() {
    const that = this
    // 如果用户未登录
    if (this.data.token.length === 0) {
      wx.showModal({
        title: "提示",
        content: "请先登录！"
      })
    } else {
      wx.navigateTo({
        url: '../userAdmin/userAdmin',
      })
    }
  },

  bindBorrowAdmin() {
    const that = this
    // 如果用户未登录
    if (this.data.token.length === 0) {
      wx.showModal({
        title: "提示",
        content: "请先登录！"
      })
    } else {
      wx.navigateTo({
        url: '../borrowAdmin/borrowAdmin',
      })
    }
  },

  bindAddAdmin() {
    if (!this.data.isSuperAdmin) {
      wx.showToast({
        title: '您不是超级管理员，无法操作！',
      })
      return
    }

    wx.navigateTo({
      url: '../addAdmin/addAdmin',
    })

  },

  onLoad: function () {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.setData({
      isSuperAdmin: wx.getStorageSync('isSuperAdmin')
    })
  },

  onShow: function () {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.setData({
      isSuperAdmin: wx.getStorageSync('isSuperAdmin')
    })
  }

})